//
//  LSZSignListViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/6/8.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZSignListViewController.h"

@interface LSZSignListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableViewList;
@property(nonatomic,assign) NSInteger page;

@property(nonatomic,strong) NSMutableArray * arrayDataAdd;//
@end

@implementation LSZSignListViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.page = 1;
    [self getPageData :self.page];
}
-(void)getPageData :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic userCheckCharts:@"0" page:[NSString stringWithFormat:@"%zd",(long)page]  :^(BOOL isOk, NSString * string, LSZCheckChartsManager * manager) {
        if (isOk == YES) {
            if (weakSelf.page == 1) {
                [weakSelf.arrayDataAdd removeAllObjects];
                if(manager.arrayData.count == 0){
                    weakSelf.tableViewList.mj_footer.hidden = YES;
                    LSZEmptyDataVC * vc = [[LSZEmptyDataVC alloc]init];
                    [vc showWithView:weakSelf.view];
                }else{
                    [weakSelf.arrayDataAdd setArray:manager.arrayData];
                }
            }
            else{
                [weakSelf.arrayDataAdd addObjectsFromArray:manager.arrayData];
            }
            if(manager.arrayData.count == 0){
                [weakSelf.tableViewList.mj_footer endRefreshingWithNoMoreData];
            }
            [weakSelf.tableViewList reloadData];
        }else{
            [weakSelf setStringWarning:string];
        }
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"签到总排行榜";
    self.arrayDataAdd = [[NSMutableArray alloc]init];
    [self.tableViewList registerClass:[LSZListTableViewCell class] forCellReuseIdentifier:@"LSZListTableViewCell"];
    __weak typeof(self) weakSelf = self;
    weakSelf.tableViewList.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            weakSelf.page = 1;
            [weakSelf getPageData:weakSelf.page];
            [weakSelf.tableViewList.mj_header endRefreshing];
        });
    }];
    // 上拉刷新
    weakSelf.tableViewList.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            weakSelf.page++;
            [weakSelf getPageData:weakSelf.page];
            [weakSelf.tableViewList.mj_footer endRefreshing];
        });
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayDataAdd.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (ScreenWidth == 320) {
        //5
        return 45;
    }else if(ScreenWidth == 375){
        // 6 7
        return 50;
    }else {
        //plus
        return 58;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZListTableViewCell" forIndexPath:indexPath];
    LSZCheckChartsMg * mg = nil;
    mg = self.arrayDataAdd[indexPath.row];
    [cell.imageHeader sd_setImageWithURL:[NSURL URLWithString:mg.avatar] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cell.labelName.text = mg.username;
    cell.labelNumber.text = [NSString stringWithFormat:@"累计%@次",mg.count];
    if (indexPath.row == 0||indexPath.row == 1 ||indexPath.row == 2) {
        cell.number.hidden = YES;
        cell.imageViewGoldMedal.hidden = NO;
        cell.imageViewGoldMedal.image = [UIImage imageNamed:[NSString stringWithFormat:@"排行榜第%zd",(long)indexPath.row+1]];
    }else{
        cell.number.hidden = NO;
        cell.imageViewGoldMedal.hidden = YES;
        cell.number.text = [NSString stringWithFormat:@"%zd",(long)indexPath.row+1];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(UITableView *)tableViewList
{
    if (_tableViewList == nil) {
        _tableViewList = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-HOME_INDICATOR_HEIGHT) style:UITableViewStyleGrouped];
        _tableViewList.delegate = self;
        _tableViewList.dataSource = self;
        _tableViewList.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        _tableViewList.estimatedRowHeight =0;
        _tableViewList.estimatedSectionFooterHeight =0;
        _tableViewList.estimatedSectionHeaderHeight =0;
        [self.view addSubview:_tableViewList];
    }
    return _tableViewList;
}


@end
